package com.zyf.customview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.zyf.customview.R;

/**
 * 作者：Think
 * 创建于 2017/3/6 16:08
 */

public class PlaneProgressView extends View {
    /**
     * 背景画笔
     * */
    private Paint mPaint;
    /**
     * 进度画笔
     * */
    private Paint progressPaint;
    /**
     * 飞机画笔
     * */
    private Paint planePaint;
    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;
    /**
     * 上下文对象
     * */
    private Context context;
    /**
     * 飞机图像
     * */
    private Bitmap bitmap;
    /**
     * 飞机图像高度
     * */
    private int bitmapHeight;
    /**
     * 飞机图像宽度
     * */
    private int bitmapWidth;
    private int paddingPx = 100;
    /**
     * 进度
     * */
    private int currentProgress = 50;
    /**
     * 进度步数
     * */
    private float progressStep = 0f;

    public PlaneProgressView(Context context) {
        this(context,null);
    }

    public PlaneProgressView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PlaneProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.plan);
        bitmapHeight = bitmap.getHeight();
        bitmapWidth = bitmap.getHeight();
        init();
    }
    /**
     * 初始化数据
     * */
    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#006493"));
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(30);
        progressPaint = new Paint();
        progressPaint.setColor(Color.parseColor("#4CBDE8"));
        progressPaint.setAntiAlias(true);
        progressPaint.setStrokeWidth(12);
        planePaint = new Paint();
        planePaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       // canvas.drawColor(Color.parseColor("#0E93C5"));
        drawProgressBackGround(canvas);
        drawProgressSpeed(canvas);
        canvas.drawBitmap(bitmap,paddingPx+currentProgress*progressStep-bitmapWidth/2,height/2-bitmapHeight/2+bitmapHeight/10,planePaint);
    }



    /**
     * 画进度条的背景
     * */
    private void drawProgressBackGround(Canvas canvas) {
        canvas.drawLine(paddingPx,height/2,width-paddingPx,height/2,mPaint);
        canvas.drawCircle(paddingPx,height/2,15,mPaint);
        canvas.drawCircle(width-paddingPx,height/2,15,mPaint);
    }
    /**
     * 画进度条的前景
     * */
    private void drawProgressSpeed(Canvas canvas) {
        canvas.drawLine(paddingPx,height/2,paddingPx+currentProgress*progressStep,height/2,progressPaint);
        canvas.drawCircle(paddingPx,height/2,6,progressPaint);
        canvas.drawCircle(paddingPx+currentProgress*progressStep,height/2,6,progressPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureWidthSize(widthMeasureSpec);
        height = measureHeightSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
        progressStep = (width-2*paddingPx)/100.0f;
        Log.e("width",width+"");
        Log.e("progressStep",progressStep+"");
    }
    private int measureHeightSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = 100;
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }
    private int measureWidthSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();     // 屏幕宽度（像素）
            length = width;
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }
    /**
     * 设置当前进度
     * */
    public void setCurrentProgress(int currentProgress) {
        this.currentProgress = currentProgress;
        invalidate();
    }
}
